import { defineStore } from "pinia";
import { computed, ComputedRef, Ref, ref } from "vue";
import { fetchTrademarkListAPI, deleteTrademarkAPI, addTrademarkAPI, updateTrademarkAPI } from "@/api/product/trademarkApi/trademark.ts";
import { pageParamsInterface } from "@/views/product/trademark/interface/interface.ts";
import { trademark, trademarkInfo } from "@/api/product/trademarkApi/interface.ts";
import { trademarkStore } from "@/pinia/modules/product/trademarkStore/interface.ts";

const useTrademarkStore = defineStore("trademarkStore", (): trademarkStore => {
  /* 品牌信息 */
  const trademarkInfo: Ref<trademarkInfo | null> = ref<trademarkInfo | null>(null);
  /* 品牌列表 */
  const trademarkList: ComputedRef<trademark[] | undefined> = computed(() => trademarkInfo.value?.records);
  /* 总共的品牌数据条数 */
  const total: ComputedRef<number | undefined> = computed(() => trademarkInfo.value?.total);
  /* 发送请求获取品牌的回调函数 */
  const fetchTrademarkList: (pageParams: pageParamsInterface) => Promise<void> = async (pageParams: pageParamsInterface): Promise<void> => {
    const { data }: { data: trademarkInfo } = await fetchTrademarkListAPI(pageParams);
    trademarkInfo.value = data;
  };
  /* 发送请求删除品牌的回调函数 */
  const deleteTradeMark: (id: number) => Promise<string> = async (id: number): Promise<string> => {
    await deleteTrademarkAPI(id);
    return "删除品牌成功";
  };
  /* 发送请求添加品牌的回调函数 */
  const addTradeMark: (trade: trademark) => Promise<string> = async (trade: trademark): Promise<string> => {
    await addTrademarkAPI(trade);
    return "添加品牌成功";
  };

  /* 发送请求修改品牌的回调函数 */
  const updateTradeMark: (trade: trademark) => Promise<string> = async (trade: trademark): Promise<string> => {
    await updateTrademarkAPI(trade);
    return "修改品牌成功";
  };

  return {
    trademarkInfo,
    trademarkList,
    total,
    fetchTrademarkList,
    deleteTradeMark,
    addTradeMark,
    updateTradeMark,
  };
});

export default useTrademarkStore;
